<template>
<div>
<setHeader :title="headerTitle" settext="添加" @done="gotobtn()"></setHeader>

  <div class="content">
    <div v-if="applyList == ''" class="contentEmpty" style="margin-top: 1.2rem;">
        <img src="../common/images/Disappointment.png" alt="">
        <em>暂无查询结果</em>
    </div>
    <div class="business_lists" v-for="apply in applyList" :key="apply.id" v-else>
      <div class="business_left"><img :src="apply.avatar" alt=""></div> 
      <div class="business_desc"><h5>{{apply.username}}</h5> <p>{{apply.reason}}</p></div> <div class="btn_small_red">
        <button @click='handleAgree(apply)' v-show="parseInt(apply.status)===0">同意</button>
        <button @click='handleReject(apply)' v-show="parseInt(apply.status)===0">拒绝</button>
        <button :disabled="parseInt(apply.status) === 1 " v-show="parseInt(apply.status)===1">已同意</button>
        <button :disabled="parseInt(apply.status) === 2 " v-show="parseInt(apply.status)===2">已拒绝</button>
        </div>
    </div>
  </div>
	  <so-footer></so-footer>
</div>
  
</template>

<script>
import setHeader from '../public/setHeader.vue';
import soFooter from '../components/footer-bar';
import {ws,createApply,applyList, updateApply, updateUser,createChat} from '../api/api'
  export default {
    data() {
      return {
        headerTitle: '新的朋友',
        haveNotReadApply: false,
        applyList: []
      }
    },
    components: {
      // soHeader,
      setHeader,
      soFooter,
      // headerSearch
      // headerText
    },
    methods: {
      
      //好友申请列表
      handleApplyList () {
        applyList().then(res => {
          if (parseInt(res.status) === 1) {
            console.log('01',res);
            this.applyList = res.applyList;
            for (let x in this.applyList) {
              if (parseInt(this.applyList[x].is_read) === 0) {
                this.$emit('update:notReadApply', true)
                break
              }
            }
          }
        })
      },
      // 同意好友申请
      handleAgree (apply) {
        updateApply({status: 1, id: apply.id}).then(res => {
          if (parseInt(res.status) === 1) {
            console.log('好友信息',apply.sponsor_id);
            // this.$emit('handleFriendList')
            apply.status = 1
            //创建一个聊天对象
            createChat({targetId: apply.sponsor_id}).then(respose => {
              console.log('同意好友申请',respose);
            if (parseInt(respose.status) === 1) {
              respose.chat.user_id = apply.target_id;
              console.log('000',respose.chat);
              // chat = respose.chat
              // 切换聊天对象
              this.$router.push({
                path: '/chat',
                query: {
                  chat: respose.chat,
                  type: 'new'
                }
              });
              // this.chatList.unshift(chat)
              // this.switchInterface('chat')  // 切换到聊天界面
              // this.changeChat(chat)    // 将当前聊天对象切换成该聊天
            }
          })
          }
        })
      },
      // 拒绝好友申请
      handleReject (apply) {
        updateApply({status: 2, id: apply.id}).then(res => {
          if (parseInt(res.status) === 1) {
            apply.status = 2
          }
        })
      },
      //点击添加触发事件
      gotobtn (){
        this.$router.push({
            path: '/information/addfriend',
        });
      },
    },
    computed: {
      // 好友申请搜索
      filterApplyList () {
        if (this.search !== '') {
          return this.applyList.filter((element) => {
            return element.username.indexOf(this.search) !== -1
          })
        }
        return this.applyList
      },
      show () {
        this.handleApplyList()
        return this.visible
      }
    },
    mounted () {
      this.handleApplyList();
      this.$store.commit('setPage', 2);//设置需要预请求的参数
    },
    
  }

</script>
<style >


</style>
